package com.sanhuo.triild.utils;

import com.sanhuo.triild.annotation.Result;
import com.sanhuo.triild.annotation.Results;
import com.sanhuo.triild.entity.ResultsDetail;
import com.sanhuo.utils.basic.CollectionUtil;
import com.sanhuo.utils.basic.ReflectionUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ResultsUtil {
    private static final String PROPERTY = "property";//对应实体类的字段
    private static final String COLUMN = "column";//数据库查出来的字段
    private static final String JAVATYPE = "javaType";

    public static List<ResultsDetail> getResults(Results results) {
        List<ResultsDetail> detail = new ArrayList<>();
        Result[] resultList = results.value();
        if (CollectionUtil.isEmpty(resultList)) {
            return null;
        } else {
            for (Result result : resultList) {
                ResultsDetail resultsDetail = new ResultsDetail();
                resultsDetail.setProperty(result.property());
                resultsDetail.setColumn(result.colomn());
                resultsDetail.setJavaType(result.javaType());
                detail.add(resultsDetail);
            }
            return detail;
        }
    }


    public static Object toResults(Results results, Class clazz, Map<String, Object> map) {
        List<ResultsDetail> detail = getResults(results);
        Object entity = null;
        try {
            entity = clazz.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        if (ReflectionUtil.isPrimitive(clazz)) {
            String key = (String) map.keySet().toArray()[0];
            return map.get(key);
        } else {
            for (String key : map.keySet()) {
                for (ResultsDetail resultsDetail : detail) {
                    if (resultsDetail.getColumn().equals(key)) {
                        String property = resultsDetail.getProperty();
                        if (ReflectionUtil.isExistsFielt(entity, property)) {
                            ReflectionUtil.setFieldValueForce(entity, property, map.get(resultsDetail.getColumn()));
                        }
                    } else {
                        if (ReflectionUtil.isExistsFielt(entity, key)) {
                            ReflectionUtil.setFieldValueForce(entity, key, map.get(key));
                        }
                    }
                }
            }
        }
        return entity;
    }

    public static Object mapToEntity(Class clazz, Map<String, Object> map) {
        Object entity = null;
        try {

            if(clazz.getName().equals("java.lang.Double")){
                entity = new Double(0);
            }else
            if(clazz.getName().equals("java.lang.Integer")){
                entity = new Integer(0);
            }else{
                entity = clazz.newInstance();
            }
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        if (ReflectionUtil.isPrimitive(clazz)) {
            String key = (String) map.keySet().toArray()[0];
            return map.get(key);
        } else {
            for (String key : map.keySet()) {
                if (ReflectionUtil.isExistsFielt(entity, key)) {
                    ReflectionUtil.setFieldValueForce(entity, key, map.get(key));
                }
            }
        }
        return entity;
    }

}
